package com.wukaige.order.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wukaige.order.config.JwtUtils;
import com.wukaige.order.config.ResponseResult;
import com.wukaige.order.entity.Goods;
import com.wukaige.order.entity.Order;
import com.wukaige.order.entity.Ordergoods;
import com.wukaige.order.entity.User;
import com.wukaige.order.feign.GoodsFeign;
import com.wukaige.order.feign.UserFeign;
import com.wukaige.order.service.IOrderService;
import com.wukaige.order.service.IOrdergoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wukaige
 * @since 2021-05-05
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    IOrderService orderService;

    @Autowired
    IOrdergoodsService ordergoodsService;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    GoodsFeign goodsFeign;

    @Autowired
    UserFeign userFeign;
    
    @RequestMapping("postdetail")
    public ResponseResult postdetail(@RequestBody Order orders, HttpServletRequest request) {

        Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent(orders.getUuid(), "", Duration.ofSeconds(15));
        if (!aBoolean) {
            return ResponseResult.fail("请勿重复提交订单");
        }
        JwtUtils jwtUtils = new JwtUtils();
        String token = request.getHeader("token");
        String userIdByToken = jwtUtils.getUserIdByToken(token);
        System.out.println(userIdByToken);
        orders.setUid(Integer.parseInt(userIdByToken));
        System.out.println(orders.getUid() + "===");
        orders.setStatus(1);
        orders.setCretime(new Date());

        //验证商品库存是否充足
        for (Ordergoods ordergoods : orders.getDetail()) {
            System.out.println(ordergoods.getGid());
            Goods getgoodsbygid = goodsFeign.getgoodsbygid(ordergoods.getGid());
            System.out.println("getgoodsbygid==========" + getgoodsbygid);
            if (getgoodsbygid.getGoodsum() - ordergoods.getGnumber() < 0) {
                return ResponseResult.fail("本页面中有部分商品库存不足,请返回购物车修改");
            }
        }
        orderService.save(orders);


        //添加中间表
        for (Ordergoods ordergoods : orders.getDetail()) {
            ordergoods.setOid(orders.getOid());
            ordergoodsService.save(ordergoods);
        }


        //添加成功之后从相应的商品表中删除该库存相应的数量
        for (Ordergoods ordergoods : orders.getDetail()) {
            Goods getgoodsbygid = goodsFeign.getgoodsbygid(ordergoods.getGid());
            getgoodsbygid.setGoodsum(getgoodsbygid.getGoodsum() - ordergoods.getGnumber());
            Integer goodsum = getgoodsbygid.getGoodsum();
            Integer invent = ordergoods.getGnumber();
            boolean updatebygid = goodsFeign.updatebygid(getgoodsbygid.getGid(), goodsum,invent);
            System.out.println("修改库存成功:" + updatebygid);
        }

        return ResponseResult.success(orders);
    }

    @RequestMapping("postuuid")
    public ResponseResult postuuid() {
        return ResponseResult.success(UUID.randomUUID() + "");
    }


    @RequestMapping("getorderlist")
    public IPage getorderlist(Page page, Order order, HttpServletRequest request) {
        JwtUtils jwtUtils = new JwtUtils();
        String token = request.getHeader("token");
        String userIdByToken = jwtUtils.getUserIdByToken(token);
        int uid = Integer.parseInt(userIdByToken);
        IPage pageinfo = orderService.getorderlist(page, order, uid);
        List<Order> records = pageinfo.getRecords();
        for (Order record : records) {
            User user = userFeign.getunamebyuid(record.getUid());
            record.setUname(user.getUname());
        }
        return pageinfo;
    }

    @RequestMapping("updatestatus")
    public ResponseResult updatestatus(@RequestBody Order order) {
        System.out.println(order);
        boolean b = orderService.updateByOid(order.getOid());
        return ResponseResult.success(b);
    }
    @RequestMapping("delordersbyoid")
    public ResponseResult delordersbyoid(@RequestBody Order order) {
        boolean b = orderService.removeByOid(order.getOid());
        return ResponseResult.success(b);
    }

}

